package hoowin.basic.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

import hoowin.basic.dao.IAccountDao;
import hoowin.basic.dao.IFollowDao;
import hoowin.basic.dao.IUserDao;
import hoowin.basic.model.Account;
import hoowin.basic.model.User;
import hoowin.basic.util.Encoder;
import hoowin.basic.util.Validator;
import hoowin.common.service.MailService;

public class AccountService {
	private final String encoding = "utf-8";
	private IAccountDao accountDao;
	private MailService mailService;
	private IUserDao userDao;
	private IFollowDao followDao;
	public int getInvitorByEmail(String email){
		if(email==null || email.length()==0)return 0;
		String code;
		try {
			code = Encoder.encryptMD5(email);
			return accountDao.getInvitorByCode(code);
		} catch (NoSuchAlgorithmException e) {
			return 0;
		}
	}
	public boolean sendInvitations(){
		List<String> emails = accountDao.getRemainEmails();
		Iterator<String> it = emails.iterator();
		User user = new User();
		user.setId(10101210);
		user.setName("Dennis");
		while(it.hasNext()){
			saveInvitation(it.next(), user);
		}
		return true;
	}
	private String encodeFromEmail(String email){
		if(email==null || email.length()==0)return null;
		try {
			byte[] data = email.getBytes(encoding);
			return Encoder.encryptBASE64(data);
		} catch (UnsupportedEncodingException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
		
	}
	public String decodeToEmail(String code){
		if(code==null||code.length()==0)return null;
		byte[] data;
		try {
			data = Encoder.decryptBASE64(code);
		} catch (IOException e1) {
			return null;
		}
		try {
			return new String(data, encoding);
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	public String saveInvitation(String email, User user){
		if(Validator.isEmailAddress(email, true)){
			try {
				if(accountDao.saveInvitation(Encoder.encryptMD5(email),user.getId())){
					String subject = "invitation from " + user.getName();
					String content = "Hello, "+user.getName()+
					" invites you to join woshini.de. Please sign up with your true name. Click http://woshini.de/basic/createAccount.action?phase=1&code="+this.encodeFromEmail(email);
					mailService.sendSimpleMail(subject, content, email);
					return "success";
				}
				else return "error.1007";
			} catch (NoSuchAlgorithmException e) {
				return "exception.1000";
			}
		}else{
			return "error.1003";
		}
	}
	public String createAccount(Account acc, String name, String language, String sex, int invitorId){
		try {
			acc.setPassword(Encoder.encryptMD5(acc.getPassword()));
		} catch (NoSuchAlgorithmException e) {
			return "exception.1000";
		}
		int id = accountDao.addAccount(acc);
		if(id==0)return "error.1008";
		try {
			if(!accountDao.deleteInvitation(Encoder.encryptMD5(acc.getEmail())))return "error.100a";
		} catch (NoSuchAlgorithmException e) {
			return "exception.1000";
		}
		if(userDao.addUser(id, name, sex, language, invitorId)){
			followDao.addFollow(id, invitorId, 1);
			followDao.addFollow(invitorId, id, 1);
			if(invitorId!=10101219)followDao.addFollow(10101219, id, 1);
			return "success";
		}
		else return "error.1009";
	}
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}
	public void setAccountDao(IAccountDao accountDao){
		this.accountDao = accountDao;
	}
	public MailService getMailService() {
		return mailService;
	}
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	public IUserDao getUserDao() {
		return userDao;
	}
	public void setFollowDao(IFollowDao followDao) {
		this.followDao = followDao;
	}
	public IFollowDao getFollowDao() {
		return followDao;
	}
}
